package com.cheer.ba.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.utils.CachePath;
import com.cheer.ba.utils.ClearCacheUtil;
import com.cheer.ba.utils.CountDownTimer;
import com.cheer.ba.utils.DataCleanManager;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.dialog.DialogUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置界面
 *
 * @author HQ
 *         created at 2017/3/14 0014 13:26
 */
public class SettingAty extends BaseCommAty {

    //修改密码
    @BindView(R.id.id_modify_pw_rela)
    RelativeLayout mRelaModifyPw;

    //緩存大小
    @BindView(R.id.id_clear_cache_txt)
    TextView mTxtCache;

    //退出按钮
    @BindView(R.id.id_app_exit_btn)
    Button mBtnExit;

    //缓存处理
    private String mCacheSize;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_setting, null));
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.mine_setting));
        if (Session.getUserIsLogin()) {
            mRelaModifyPw.setVisibility(View.VISIBLE);
            mBtnExit.setVisibility(View.VISIBLE);
        } else {
            mRelaModifyPw.setVisibility(View.GONE);
            mBtnExit.setVisibility(View.GONE);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        calculateCache();
    }

    /**
     * 计算缓存大小
     */
    private void calculateCache() {
        try {
            //外部存储数据大小
            long wai = DataCleanManager.getFolderSize(new File(
                    CachePath.wai_path));
//            long nei = DataCleanManager.getFolderSize(new File(
//                    CachePath.nei_path));
//            long databases = DataCleanManager.getFolderSize(new File(
//                    CachePath.databases_path));
            //shareprefresh存储数据大小
            long share = DataCleanManager.getFolderSize(new File(
                    CachePath.share_path));
            long cache = wai + share;
            if (cache == 0) {
                mCacheSize = "0.00B";
            } else {
                mCacheSize = DataCleanManager.getFormatSize(cache);
            }
            mTxtCache.setText(mCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.id_modify_pw_rela, R.id.id_clear_app_cache, R.id.id_about_us_view, R.id.id_app_exit_btn})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //修改登录密码
            case R.id.id_modify_pw_rela:
                Intent modifyPW = new Intent(aty, ModifyPwAty.class);
                startActivityForResult(modifyPW, IntentCode.Forget.MODIFY_PW_REQUEST_CODE);
                break;
            //清理本地缓存
            case R.id.id_clear_app_cache:
                ClearCacheUtil.clearCache(aty, 0, "");
                mTxtCache.setText("0M");
                SVProgressHUD.showSuccessWithStatus(aty, getString(R.string.clear_cache_success));
                break;
            //关于加油啦
            case R.id.id_about_us_view:
                showActivity(aty, AboutCheerAty.class);
                break;
            //退出
            case R.id.id_app_exit_btn:
                View dialogView = LayoutInflater.from(aty).inflate(R.layout.login_remind, null);
                final Dialog dialog = DialogUtils.selfDialog(aty, dialogView, true);
                dialog.show();
                ((TextView) dialogView.findViewById(R.id.id_remind_login_txt)).setText(getString(R.string.exit_app_remind));
                ((TextView) dialogView.findViewById(R.id.id_login_confirm_txt)).setText(getString(R.string.mine_exit));
                dialogView.findViewById(R.id.id_login_confirm_txt).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (CountDownTimer.isFastClick()) {
                            return;
                        }
                        dialog.dismiss();
                        exitApp();
                    }
                });
                dialogView.findViewById(R.id.id_login_cancel_txt).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                break;
        }
    }

    public void exitApp() {
        ClearCacheUtil.clearCache(aty, 1, Session.getUserId());
        //退出成功
        BaseEvent event = new BaseEvent();
        event.type = IntentKey.Login.KEY_APP_EXIT;
        EventBus.getDefault().post(event);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IntentCode.Forget.MODIFY_PW_REQUEST_CODE) {
            if (resultCode == IntentCode.Forget.MODIFY_PW_RESULT_CODE) {
                //修改登录密码成功
                BaseEvent event = new BaseEvent();
                event.type = IntentKey.Login.KEY_MODIFY_LOGIN_PW;
                EventBus.getDefault().post(event);
                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
